<template>
  <div class="detail">
    <van-nav-bar
      left-text="返回"
      :title="product.name"
      right-text="我的"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />

    <div class="content" v-html="product.content"></div>
    <van-goods-action>
      <van-goods-action-icon
        icon="home-o"
        text="首页"
        @click="onClickIcon('Home')"
      />
      <van-goods-action-icon
        icon="cart-o"
        text="购物车"
        @click="onClickIcon('Cart')"
      />
      <van-goods-action-icon
        icon="fire-o"
        text="热卖"
        @click="onClickIcon('List')"
      />
      <van-goods-action-button
        type="danger"
        text="加入购物车"
        @click="buyHandle"
      />
    </van-goods-action>
  </div>
</template>

<script>
import { Notify } from "vant";
import { addToCartAPI } from "../services/carts";
import { loadProductDetailAPI } from "../services/product";
export { addToCartAPI } from "../services/carts";
export default {
  name: "Detail",
  data() {
    return {
      product: {},
    };
  },
  async created() {
    const res = await loadProductDetailAPI(this.$route.query.id);
    this.product = res.data;
  },
  methods: {
    //跳转
    onClickIcon(name) {
      this.$router.push({
        name,
      });
    },
    async buyHandle() {
      const res = await addToCartAPI(this.product.id, 1, this.product.price);
      // console.log(res);
      if (res.code === 1) {
        Notify({ type: "success", message: res.msg });
        this.$router.push({
          name: "Cart",
        });
      } else {
        Notify({ type: "warning", message: res.data });
      }
    },
    onClickLeft() {
      this.$router.go(-1);
    },
    onClickRight() {
      this.$router.push({
        name: "User",
      });
    },
  },
};
</script>

<style></style>
